home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Classes / HashFile / HashExample.m < prev    next >
Text File  |  1995-06-12  |  3KB  |  112 lines

  1. /* File: HashExample.m - HashFile Example ('phone number to word translator)
  2.  *
  3.  * By: Christopher Lane
  4.  * Symbolic Systems Resources Group
  5.  * Knowledge Systems Laboratory
  6.  * Stanford University
  7.  *
  8.  * Date: 22 March 1991
  9.  *
  10.  * Copyright: 1990, 1991 by The Leland Stanford Junior University.  This
  11.  * program may be distributed without restriction for non-commercial use.
  12.  */
  13.  
  14. #import <stdlib.h>
  15. #import <stdio.h>
  16. #import <string.h>
  17. #import <ctype.h>
  18. #import <getopt.h>
  19.  
  20. #import "HashFile.h"
  21.  
  22. extern char *basename(char *s); // #import <text/pathutil.h>
  23.  
  24. #define INTEGER @encode(int)
  25. #define STRING @encode(char *)
  26.  
  27. #define OPTIONSTRING "bdh:"
  28. #define USAGE "usage: %s [-h database] [-d] [-b < wordlist] \n"
  29.  
  30. typedef enum { BUILD = 'b', DUMP = 'd', HASHFILE = 'h' } OPTIONS;
  31. typedef enum { PROGRAM } ARGUMENTS;
  32.  
  33. #define BUFFERSIZE 1024
  34. #define DIGITMAX (7)
  35.  
  36. #ifndef DATABASE
  37. #define DATABASE "PhoneWords"
  38. #endif
  39.  
  40. const char *letters = "abcdefghijklmnoprstuvwxyABCDEFGHIJKLMNOPRSTUVWXY";
  41. const char *numbers = "222333444555666777888999222333444555666777888999";
  42.  
  43. void buildTable(id table)
  44. {        
  45.     char *s, *idx = NULL, value[BUFFERSIZE], buffer[BUFFERSIZE];
  46.     unsigned int key;
  47.         
  48.     [table empty];
  49.  
  50.     while(gets(value) != NULL) {
  51.         if(strlen(s = value) > DIGITMAX) continue;
  52.         key = 0;
  53.         while(*s)
  54.             if((idx = index(letters, *s++)) == NULL) break;
  55.             else key = (key * 10) + ( numbers[idx - letters] - '0' );
  56.         if(idx == NULL) continue;
  57. #ifdef DEBUG
  58.         printf("<%d\t%s\n", key, value);
  59. #endif
  60.         if([table isKey:(void *) key])
  61.             s = strcat(strcat(strcpy(buffer, (char *) [table valueForKey:(void *) key]), " "), value);
  62.         else s = value;
  63.         
  64.         [table insertKey:(void *) key value:(void *) NXCopyStringBuffer(s)];
  65.         } /* while */
  66. }
  67.  
  68. void dumpTable(id table)
  69. {
  70.     void *key, *value;
  71.     NXHashState state = [table initState];
  72.  
  73.     while([table nextState:&state key:&key value:&value])
  74.         (void) printf("%d\t%s\n", (int) key, (char *) value);
  75. }
  76.  
  77. void main(int argc, char *argv[])
  78. {
  79.     id table;
  80.     int key, option, nitems, status = EXIT_SUCCESS;
  81.     char *database = DATABASE;
  82.     BOOL buildFlag = NO, dumpFlag = NO;
  83.         
  84.     while((option = getopt(argc, argv, OPTIONSTRING)) != EOF)
  85.         switch(option) {
  86.         case BUILD : buildFlag = YES; break;
  87.         case DUMP : dumpFlag = YES; break;
  88.         case HASHFILE : database = optarg; break;
  89.         default : status = EXIT_FAILURE;
  90.         } /* while */
  91.  
  92.     if(optind < argc || status == EXIT_FAILURE) {
  93.         (void) fprintf(stderr, USAGE, basename(argv[PROGRAM]));
  94.         exit(EXIT_FAILURE);
  95.         } /* if */
  96.  
  97.     if((buildFlag || [HashFile isHashFile:database]) &&
  98.        (table = [[HashFile alloc] initFromFile:database keyDesc:INTEGER valueDesc:STRING]) != nil) {
  99.         if(buildFlag) buildTable(table);
  100.         if(dumpFlag) dumpTable(table);
  101.         if(!buildFlag && !dumpFlag) {
  102.             while((nitems = scanf("%d", &key)) != EOF && nitems == 1)
  103.                 if([table isKey:(void *) key])
  104.                     (void) printf("%d\t%s\n", key, (char *) [table valueForKey:(void *) key]);
  105.             } /* if */
  106.         [table free];
  107.         } /* if */
  108.     else (void) fprintf(stderr, "%s: Can't open %s.\n", basename(argv[PROGRAM]), database, status = EXIT_FAILURE);
  109.  
  110.     exit(status);
  111. }
  112.